Online-Academy
Look, Read, Understand, Apply

PHP

Q and A - I

  1. What is PHP?

    PHP stands for Hypertext Preprocessor. It is a server-side scripting language used to develop dynamic and interactive web pages.

  2. What symbol is used to start and end a PHP script?

    <?php to start and ?> to end.

  3. How do you display text in PHP?

    Using echo or print. Example: echo "Hello World!";

  4. What is the difference between echo and print?

    Both output text, but print returns a value (1) and can be used in expressions, while echo cannot.

  5. How do you write a single-line comment in PHP?

    Using // or #. Example: // This is a comment

  6. What are variables in PHP?

    Variables store data values and start with $. Example: $name = "Dinesh";

  7. How do you concatenate strings in PHP?

    Using the . (dot) operator. Example: echo "Hello " . "World!";

  8. What are PHP data types?

    String, Integer, Float, Boolean, Array, Object, NULL, and Resource.

  9. How do you define a constant in PHP?

    Using define("NAME", "value");

  10. What is the difference between == and ===?

    == checks value equality, while === checks both value and data type.

  11. What function is used to get the data type of a variable?

    gettype($variable);

  12. How can you get the length of a string in PHP?

    strlen("Hello");

  13. What is an associative array?

    An array that uses named keys instead of numeric indexes. Example: $age = ["Ram" => 25, "Sita" => 23];

  14. How do you include a file in PHP?

    Using include 'file.php'; or require 'file.php';

  15. What is the difference between include and require?

    If a file is missing, include shows a warning and continues execution; require stops execution with a fatal error.

  16. How do you connect to a MySQL database in PHP (MySQLi)?

    $conn = mysqli_connect("localhost", "root", "", "test");

  17. How do you check if the connection was successful?

    if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }

  18. How do you fetch data from a MySQL table in PHP?
    $result = mysqli_query($conn, "SELECT * FROM users");
    while($row = mysqli_fetch_assoc($result)) {
        echo $row['name'];
    }
    
  19. What is the use of isset() function?

    Checks if a variable is set and not null.

  20. What is the use of empty() function?

    Checks whether a variable is empty.

  21. How do you start a session in PHP?

    session_start();

  22. How do you set and access session variables?

    • $_SESSION['user'] = "Dinesh";
    • echo $_SESSION['user'];

  23. How do you destroy a session?

    session_destroy();

  24. What is a cookie in PHP?

    A small piece of data stored on the client's browser.

  25. How do you set a cookie in PHP?

    setcookie("user", "Dinesh", time()+3600);